package com.avic.rong.fragment.fragmentshop.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.avic.rong.AdvertismentListBean;
import com.avic.rong.BaseFragment;
import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.MineLikeAdapter;
import com.avic.rong.adapter.WebBannerAdapter;
import com.avic.rong.adapter.WebBannerShopAdapter;
import com.avic.rong.bean.AdvBean;
import com.avic.rong.fragment.fragmentshop.api.ShopBannerBeanApi;
import com.avic.rong.fragment.fragmentshop.bean.ShopBannerBean;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.goodsSort.activity.secondListActivity.GoodsSortBzActivity;
import com.avic.rong.goodsSort.api.ContentAdvApi;
import com.avic.rong.userOrder.api.CaiLikeApi;
import com.avic.rong.userOrder.bean.CaiLikeBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.EditUtils;
import com.avic.rong.utils.SkipUtils;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.example.library.banner.BannerLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.example.minato.minastore.utils.AppUtils;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

//import com.avic.rong.goodsSort.activity.secondListActivity.GoodsSortBzActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.shop_search_editText)
    EditText shop_search_editText;
    @BindView(R.id.shop_caiLoveRv)
    public RecyclerView shopCaiLoveRv;
    @BindView(R.id.recycler_banner)
    BannerLayout recycler_banner;
    @BindView(R.id.recycler_banner_book)
    BannerLayout recycler_banner_book;
    @BindView(R.id.nest_scrollview)
    public NestedScrollView nest_scrollview;


    public SmartRefreshLayout refreshLayout;

    private List<ShopBannerBean.AdvertismentListBean> shopBannerList;
    private ArrayList<CaiLikeBean> listData = new ArrayList<>();
    private MineLikeAdapter mineLikeAdapter;
    private List<AdvBean> bookAdvBeanList;



    @Override
    protected int getContentViewId() {
        return R.layout.fragment_shop2;
    }

    @Override
    protected void initView() {
        refreshLayout=fragmentView.findViewById(R.id.refreshLayout);
        shopCaiLoveRv.setNestedScrollingEnabled(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                Logger.i("-----------drag");
                return false;
            }
        });
        getBannerData();
        caiLike();

        //软键盘点击搜索
        shop_search_editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    EditUtils.showORhideSoftKeyboard(getActivity());
                    String keyword = shop_search_editText.getText().toString();
                    if (StringUtils.isEmpty(keyword.trim())) {
                        ToastTools.showToast("请输入关键词!");
                        return true;
                    }
                    shop_search_editText.setText("");
                    startActivity(GoodsSortBzActivity.getIntent(context, keyword, GoodsSortBzActivity.ARG_PAGETYPE_SHOW_TYPE));
                    return true;
                }
                return false;
            }
        });

    }


    @OnClick({R.id.ll_group_category, R.id.shop_bz_textView, R.id.shop_ts_textView, R.id.shop_wd_textView, R.id.shop_rjgj_textView,
            R.id.shop_more,
            R.id.shop_ll_GJB, R.id.shop_ll_GB, R.id.shop_ll_HB, R.id.shop_ll_QJ, R.id.shop_ll_JJF, R.id.shop_ll_JJG})
    public void clickSkip(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.ll_group_category://分类
                Intent intent = new Intent(getContext(), GoodsSortActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.shop_bz_textView://标准
//                Intent bzIntent = new Intent(getContext(), GoodsSortBzActivity.class);
//                bzIntent.putExtra("keyword", "");
//                getActivity().startActivity(bzIntent);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1));
                break;
            case R.id.shop_ts_textView://图书
//                Intent tsIntent = new Intent(getContext(), GoodsSortTsActivity.class);
//                tsIntent.putExtra("keyword", "");
//                getActivity().startActivity(tsIntent);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 2));
                break;
            case R.id.shop_wd_textView://实用文档
//                Intent wdIntent = new Intent(getContext(), GoodsSortWdActivity.class);
//                wdIntent.putExtra("keyword", "");
//                getActivity().startActivity(wdIntent);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 3));
                break;
            case R.id.shop_rjgj_textView://软件工具
//                Intent gjIntent = new Intent(getContext(), GoodsSortGjActivity.class);
//                gjIntent.putExtra("keyword", "");
//                getActivity().startActivity(gjIntent);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 4));
                break;
            case R.id.shop_more://最新更多
//                Intent moreIntent = new Intent(getContext(), GoodsSortBzActivity.class);
//                moreIntent.putExtra("keyword", "");
//                moreIntent.putExtra("type", "时间倒序");
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 1, null, null));
//                getActivity().startActivity(moreIntent);
                break;

            case R.id.shop_ll_GJB://GJB
//                Intent gjbIntent1 = new Intent(getContext(), GoodsSortBzActivity.class);
//                gjbIntent1.putExtra("keyword", "GJB");
//                getActivity().startActivity(gjbIntent1);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 0, "1124", "1000__1"));
                break;
            case R.id.shop_ll_GB://GB
//                Intent gbIntent1 = new Intent(getContext(), GoodsSortBzActivity.class);
//                gbIntent1.putExtra("keyword", "GB");
//                getActivity().startActivity(gbIntent1);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 0, "1136", "1000__1"));
                break;
            case R.id.shop_ll_HB://HB
//                Intent hbIntent1 = new Intent(getContext(), GoodsSortBzActivity.class);
//                hbIntent1.putExtra("keyword", "HB");
//                getActivity().startActivity(hbIntent1);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 0, "1135", "1000__1"));
                break;
            case R.id.shop_ll_QJ://QJ 航天行业标准
//                Intent qjIntent1 = new Intent(getContext(), GoodsSortBzActivity.class);
//                qjIntent1.putExtra("keyword", "QJ");
//                getActivity().startActivity(qjIntent1);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 0, "1140", "1000__1"));
                break;
            case R.id.shop_ll_JJF://国家计量
//                Intent jjfIntent1 = new Intent(getContext(), GoodsSortBzActivity.class);
//                jjfIntent1.putExtra("keyword", "JJF");
//                getActivity().startActivity(jjfIntent1);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 0, "1141", "1000__1"));
                break;
            case R.id.shop_ll_JJG://检定
//                Intent jjgIntent1 = new Intent(getContext(), GoodsSortBzActivity.class);
//                jjgIntent1.putExtra("keyword", "JJG");
//                getActivity().startActivity(jjgIntent1);
                startActivity(GoodsSortBzActivity.getIntent(getContext(), 1, 0, "1145", "1000__1"));
                break;
        }
    }


    private void getBannerData() {
        ShopBannerBeanApi shopBannerBeanApi = new ShopBannerBeanApi((MainActivity) getActivity(), shopBannerBeanListener, ConstantsUtils.ADV_SHOP_TOP);
        shopBannerBeanApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(shopBannerBeanApi);
        ContentAdvApi contentAdvApi = new ContentAdvApi((MainActivity) getActivity(), bookAdvListener, ConstantsUtils.ADV_SHOP_TUSHUTUIJIANWEI);
        contentAdvApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(contentAdvApi);
    }


    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore(300);//传入false表示加载失败
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        Logger.i("----------------------dddd");
        refreshLayout.finishRefresh(300);//传入false表示刷新失败
    }


    //获取商城轮播图数据监听
    HttpOnNextListener shopBannerBeanListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(getContext(), "商品banner：" + subjects.getMsg());
                return;
            }
            ShopBannerBean shopBannerBean = (ShopBannerBean) subjects.getData();

            shopBannerList = shopBannerBean.getAdvertismentList();
            WebBannerShopAdapter webBannerAdapter = new WebBannerShopAdapter(getContext(), shopBannerList);
            recycler_banner.setAdapter(webBannerAdapter);
            webBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Logger.i("------------" + "点击了第  " + position + "  项");
                    String bannerUrl = AppUtils.COURSE_IMAGE_URL + shopBannerList.get(position).getContent();
                    SkipUtils.startSkipDetailIntent(getContext(), shopBannerList.get(position).getUrl(), bannerUrl);
                }
            });
            recycler_banner.setAutoPlaying(true);
        }


        @Override
        public void onError(final Throwable e) {
            Logger.e("cjx", "banner数据" + e.getMessage());
        }
    };

    //中部图书广告
    HttpOnNextListener bookAdvListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(getContext(), "商品banner：" + subjects.getMsg());
                return;
            }
            AdvertismentListBean advertismentListBean = (AdvertismentListBean) subjects.getData();
            bookAdvBeanList = advertismentListBean.getAdvertismentList();
            WebBannerAdapter webBannerAdapter = new WebBannerAdapter(getContext(), bookAdvBeanList);
            recycler_banner_book.setAdapter(webBannerAdapter);
            webBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Logger.i("------------" + "点击了第  " + position + "  项");
                    String bannerUrl = AppUtils.COURSE_IMAGE_URL + bookAdvBeanList.get(position).getContent();
                    SkipUtils.startSkipDetailIntent(getContext(), bookAdvBeanList.get(position).getUrl(), bannerUrl);
                }
            });
            recycler_banner_book.setAutoPlaying(true);
        }


        @Override
        public void onError(final Throwable e) {
            Logger.e("cjx", "banner数据" + e.getMessage());
        }
    };


    private void caiLike() {
        Map<String, String> map = new HashMap<>();
        map.put("token", getSharedPreferencesUtil().getLogintoken());
        CaiLikeApi caiLikeApi = new CaiLikeApi((MainActivity) getActivity(), simpleOnNextListenerCaiLike);
        caiLikeApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(caiLikeApi);
    }

    //猜你需要
    HttpOnNextListener simpleOnNextListenerCaiLike = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            listData.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(getContext(), subjects.getMsg());
                return;
            }
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
            shopCaiLoveRv.addItemDecoration(new GridSpacingItemDecoration(2, DensityUtils.dip2px(context, 10), false));
            shopCaiLoveRv.setLayoutManager(gridLayoutManager);
            listData.addAll((ArrayList<CaiLikeBean>) subjects.getData());
            mineLikeAdapter = new MineLikeAdapter(getContext(), listData);
            shopCaiLoveRv.setAdapter(mineLikeAdapter);
        }


        @Override
        public void onError(final Throwable e) {

        }
    };


    @Override
    protected void addListener() {

    }
}
